
const Web3 = require('web3');
var web3 = new Web3(new Web3.providers.HttpProvider("http://127.0.0.1:8545"));
const accounts = [{ private_key: "0x038b348d4bb5600e736e07c0f036b1be0cf3960b5a8283e5ad4db06f68a26db0", address: "0x00F8D81F9bEcF0bD291f2D069CA655B7A6f672Ef" },
{ private_key: "0xb9a2cc97715ca2faad216e482fe6a990d2e46ec1fa9d81eba9ee51f7e266e52b", address: "0x39E5b8AC05972A67C97197f964674f278E04f349" },
{ private_key: "0x47a2b5b4346ad3676722cb130b1818f4f57094ed555dc61498d8d10b0daa2e9e", address: "0xb85cA0627Fc1A08eBc41378d2328b9EA70512Db0" },
{ private_key: "0x3b5e6e00a2aa93237b946f72041358adbd5f2e4d51dab5c0a2777e6a140ebbc7", address: "0x5202C38411A4605D4e671FF530d3AA5167052234" }]

const root_account = accounts[0].address;
const ca_account = accounts[1].address;
const root_private_key = Buffer.from(accounts[0].private_key, 'hex');
const ca_private_key = Buffer.from(accounts[1].private_key, 'hex');

// get abi
const fs = require('fs');
const contractJson = JSON.parse(fs.readFileSync('/home/fredyvia/digital-identity-management/build/contracts/DigitalIdentityManagement.json', 'utf8'));
const contractAbi = contractJson.abi;
console.log(contractJson.networks["5777"].address);
var contract = new web3.eth.Contract(contractAbi, contractJson.networks["5777"].address);
// register root
contract.methods.register().send({ from: root_account, gas: 10000000 }).then(res => {
  console.log("res")
  console.log(res)
})

contract.methods.addDidMethod("cn").send({ from: root_account, gas: 10000000 }).then(res => {
  console.log(res)
})

var curr = new Date().getTime();
contract.methods.addDid({
  userAddr: ca_account,
  didMethodName: "cn",
  didId: "edu",
  issueDatetime: curr,
  validDatetime: curr + 100000000,
  issueDidSymbol: "eth:" + root_account.toLowerCase()
}).send({ from: root_account, gas: 10000000 }).then(res => {
  console.log(res)
})

contract.methods.addDidMethod("edu").send({ from: ca_account, gas: 10000000 }).then(res => {
  console.log(res)
})